home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 418_02 / doc / prepdoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-02  |  9.7 KB  |  331 lines

  1. /* prepdoc.c
  2.  * Document Preparation System
  3.  * Roger Sayle, January 1994
  4.  * Version 1.0
  5.  */
  6.  
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <ctype.h>
  10.  
  11. #ifndef True
  12. #define True  1
  13. #define False 0
  14. #endif
  15.  
  16. #define RTFForm     0x00
  17. #define LaTeXForm   0x01
  18. #define HTMLForm    0x02
  19. #define TextForm    0x03
  20. #define HelpForm    0x04
  21. #define ManForm     0x05
  22.  
  23.  
  24.  
  25. static char Buffer[82];
  26. static FILE *InFile;
  27. static int Format;
  28.  
  29.  
  30. static int ReadLine()
  31. {
  32.     register char *ptr;
  33.     register int len;
  34.     register int ch;
  35.  
  36.     if( feof(InFile) )
  37.     {   *Buffer = 0;
  38.         return( False );
  39.     }
  40.  
  41.     ptr = Buffer;
  42.     do {
  43.         ch = getc(InFile);
  44.         if( (ch=='\n') || (ch=='\r') )
  45.         {   while( ptr != Buffer )
  46.                 if( *(ptr-1)!=' ' )
  47.                 {   *ptr = 0;
  48.                     return( True );
  49.                 } else ptr--;
  50.         } else if( ch==EOF )
  51.         {   *ptr = 0;
  52.             return( True );
  53.         } else *ptr++ = ch;
  54.     } while( ptr < Buffer+80 );
  55.     *ptr = 0;
  56.  
  57.     /* skip to the end of the line! */
  58.     do { ch = getc(InFile);
  59.     } while( (ch!='\n') && (ch!='\r') && (ch!=EOF) );
  60.     return( True );
  61. }
  62.  
  63.  
  64. static int TextFlag;
  65. static int TextCol;
  66.  
  67. static void DisplayLowerCase( ptr )
  68.     char *ptr;
  69. {
  70.     register char ch;
  71.  
  72.     while( (ch = *ptr++) )
  73.         if( isupper(ch) )
  74.         {   putchar(tolower(ch));
  75.         } else putchar(ch);
  76. }
  77.  
  78. static void DisplayOnlyLowerCase( ptr )
  79.     char *ptr;
  80. {
  81.     register char ch;
  82.     
  83.     while( (ch = *ptr++) )
  84.         if( isupper(ch) )
  85.         {   putchar(tolower(ch));
  86.         } else if( ch != ' ' )
  87.             putchar(ch);
  88. }
  89.     
  90.  
  91. static void DisplayText( src )
  92.     char *src;
  93. {
  94.     register char *ptr;
  95.     register char *dst;
  96.  
  97.     while( *src )
  98.     {   dst = src;
  99.         while( *dst && *dst!=' ' )
  100.             dst++;
  101.  
  102.         if( TextCol+(dst-src) > 76 )
  103.         {   putchar('\n');
  104.             TextCol = 0;
  105.         }
  106.  
  107.         while( src != dst )
  108.         {   putchar( *src++ );
  109.             TextCol++;
  110.         }
  111.  
  112.         while( *src==' ' )
  113.             src++;
  114.         putchar(' ');
  115.         TextCol++;
  116.     }
  117. }
  118.  
  119. static void ProcessCommand()
  120. {
  121.     static char buffer[80];
  122.     register char *ptr;
  123.     register int i,len;
  124.  
  125.     ptr = Buffer+2;
  126.     switch( Buffer[1] )
  127.     {   case('R'):  if( TextCol )
  128.                         putchar('\n');
  129.                     printf("%s\n",ptr);
  130.                     TextFlag = True;
  131.                     TextCol = 0;
  132.                     break;
  133.  
  134.         case('T'):  if( Format == RTFForm )
  135.                     {   printf("%s {}\n",ptr);
  136.                         TextCol = 0;
  137.                     } else if( (Format!=TextForm) && (Format!=HelpForm) )
  138.                     {   printf("%s\n",ptr);
  139.                         TextCol = 0;
  140.                     } else DisplayText(ptr);
  141.                     TextFlag = True;
  142.                     break;
  143.  
  144.         case('P'):  if( TextFlag )
  145.                     {   if( Format==HTMLForm )
  146.                         {   fputs("<p>\n",stdout);
  147.                         } else if( Format==RTFForm )
  148.                         {   fputs("\\par\\par\n",stdout);
  149.                         } else if( (Format==TextForm) || (Format==HelpForm) )
  150.                         {   fputs("\n\n",stdout);
  151.                         } else putchar('\n');
  152.                         TextFlag = False;
  153.                         TextCol = 0;
  154.                     }
  155.                     break;
  156.  
  157.         case('B'):  if( TextFlag )
  158.                     {   if( Format==HTMLForm )
  159.                         {   fputs("<p><hr>\n",stdout);
  160.                         } else if( Format==RTFForm )
  161.                         {   fputs("\\par\\page\\par\n",stdout);
  162.                             fputs("+{\\footnote doc}\n",stdout);
  163.                         } else if( (Format==TextForm) || (Format==HelpForm) )
  164.                         {   fputs("\n\n",stdout);
  165.                         } else putchar('\n');
  166.                         TextFlag = False;
  167.                         TextCol = 0;
  168.                     }
  169.                     break;
  170.  
  171.         case('S'):  if( Format==TextForm )
  172.                     {   len = strlen(ptr);
  173.                         printf("%s\n",ptr);
  174.                         for( i=0; i<len; i++ )
  175.                             putchar('-');
  176.                         fputs("\n\n",stdout);
  177.                     } else if( Format==HTMLForm )
  178.                     {   fputs("<a name=\"",stdout);
  179.                         DisplayOnlyLowerCase(ptr);
  180.                         printf("\"><h3>%s</h3></a><p>\n",ptr);
  181.                     } else if( Format==RTFForm )
  182.                     {   fputs("#{\\footnote ",stdout);
  183.                         DisplayOnlyLowerCase(ptr);
  184.                         printf("}\n${\\footnote %s}\nK{\\footnote ",ptr);
  185.                         DisplayLowerCase(ptr);
  186.                         printf("}\n{\\b %s}\\par\\par\n",ptr);
  187.                     } else if( Format==HelpForm )
  188.                     {   putchar('?');
  189.                         DisplayLowerCase(ptr);
  190.                         printf("\n%s\n",ptr);
  191.                     } else if( Format==ManForm )
  192.                     {   printf(".TP\n.B %s\n",ptr);
  193.                     }
  194.                     TextCol = 0;
  195.                     break;
  196.  
  197.         case('X'):  while( *ptr!=' ' )
  198.                         ptr++;
  199.                     *ptr++ = 0;
  200.                     
  201.                     if( Format == RTFForm )
  202.                     {   printf("{\\uldb %s}{\\v %s} {}\n",ptr,Buffer+2);
  203.                         break;
  204.                     } else if( Format == HTMLForm )
  205.                     {   printf("<a href=\"#%s\"><tt><b>%s</b></tt></a>\n",
  206.                                Buffer+2,ptr);
  207.                         break;
  208.                     }
  209.                     
  210.         case('C'):  if( Format == RTFForm )
  211.                     {   if( *ptr=='"' )
  212.                         {   printf("\"{\\f2\\b %s}\" {}\n",ptr+1);
  213.                         } else printf("{\\f2\\b %s} {}\n",ptr);
  214.                         TextCol = 0;
  215.                     } else if( Format == HTMLForm )
  216.                     {   if( *ptr=='"' )
  217.                         {   printf("\"<tt><b>%s</b></tt>\"\n",ptr+1);
  218.                         } else printf("<tt><b>%s</b></tt>\n",ptr);
  219.                     } else if( Format == ManForm )
  220.                     {   if( *ptr=='"') ptr++;
  221.                         printf(".B %s\n",ptr);
  222.                     } else if( (Format!=TextForm) && (Format!=HelpForm) )
  223.                     {   if( *ptr=='*' )
  224.                         {   printf("\"%s\"\n",ptr);
  225.                         } else printf("`%s'\n",ptr);
  226.                         TextCol = 0;
  227.                     } else /* DisplayText! */
  228.                     {   if( *ptr=='"' )
  229.                         {   sprintf(buffer,"\"%s\"",ptr+1);
  230.                         } else sprintf(buffer,"`%s'",ptr);
  231.                         DisplayText(buffer);
  232.                     }
  233.                     TextFlag = True;
  234.                     break;
  235.     }
  236. }
  237.  
  238.  
  239. int main( argc, argv )
  240.     int argc;  char *argv[];
  241. {
  242.     register char *fname;
  243.     register char *ptr;
  244.     register int flag;
  245.  
  246.     fputs("Document Preparation System\n",stderr);
  247.     fputs("Roger Sayle, January 1994\n",stderr);
  248.     fputs("Version 1.0\n\n",stderr);
  249.  
  250.     Format = TextForm;
  251.  
  252.     if( argc==2 ) 
  253.     {   fname = argv[1];
  254.     } else if( argc==3 )
  255.     {   fname = argv[2];
  256.         ptr = argv[1];
  257.  
  258.         if( *ptr=='-' )
  259.             ptr++;
  260.  
  261.         if( !strcmp(ptr,"latex") )
  262.         {   Format = LaTeXForm;
  263.         } else if( !strcmp(ptr,"html") )
  264.         {   Format = HTMLForm;
  265.         } else if( !strcmp(ptr,"help") )
  266.         {   Format = HelpForm;
  267.         } else if( !strcmp(ptr,"rtf") || !strcmp(ptr,"mshelp") )
  268.         {   Format = RTFForm;
  269.         } else if( !strcmp(ptr,"text") || !strcmp(ptr,"ascii") )
  270.         {   Format = TextForm;
  271.         } else if( !strcmp(ptr,"man") || !strcmp(ptr,"troff") )
  272.         {   Format = ManForm;
  273.         } else
  274.         {   fputs("Formats:  -latex  LaTeX .tex file\n",stderr);
  275.             fputs("          -troff  UNIX man(1) pages\n",stderr);
  276.             fputs("          -html   HyperText metalanguage\n",stderr);
  277.             fputs("          -help   RasMol on-line help file\n",stderr);
  278.             fputs("          -rtf    Microsoft Help (Rich Text)\n",stderr);
  279.             fputs("          -text   Standard ASCII text\n\n",stderr);
  280.             exit(1);
  281.         }
  282.  
  283.     } else /* DisplayUsage */
  284.     {   fputs("Usage: prepdoc [format] <filename>\n",stderr);
  285.         exit(1);
  286.     }
  287.  
  288.     if( !(InFile=fopen(fname,"r")) )
  289.     {   fputs("Error: Unable to open input file!\n",stderr);
  290.         exit(1);
  291.     }
  292.  
  293.     TextFlag = False;
  294.     TextCol = 0;
  295.  
  296.     while( !feof(InFile) )
  297.     {   ReadLine();
  298.         switch( *Buffer )
  299.         {   case('V'):  flag = (Format==LaTeXForm) ||
  300.                                (Format==HTMLForm)  ||
  301.                                (Format==RTFForm)   ||
  302.                                (Format==TextForm);   break;
  303.  
  304.             case('D'):  flag = (Format==TextForm) ||
  305.                                (Format==HelpForm) ||
  306.                                (Format==ManForm);    break;
  307.  
  308.             case('S'):  flag = (Format==HelpForm) ||
  309.                                (Format==ManForm);    break;
  310.  
  311.             case('N'):  flag = (Format==TextForm) ||
  312.                                (Format==HelpForm);   break;
  313.  
  314.             case('L'):  flag = (Format==LaTeXForm);  break;
  315.             case('H'):  flag = (Format==HTMLForm);   break;
  316.             case('P'):  flag = (Format==HelpForm);   break;
  317.             case('T'):  flag = (Format==TextForm);   break;
  318.             case('M'):  flag = (Format==ManForm);    break;
  319.             case('R'):  flag = (Format==RTFForm);    break;
  320.             case('A'):  flag = True;                 break;
  321.             default:    flag = False;
  322.         }
  323.  
  324.         if( flag )
  325.             ProcessCommand();
  326.     }
  327.     fclose(InFile);
  328.     exit(0);
  329. }
  330.  
  331.